package server;

import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import javax.xml.bind.JAXBException;
import both.Game;
import both.StringFormatter;

public class Server extends JFrame implements ChangeListener {

    private static boolean initialChange = false;
    static JTabbedPane tabPane;
    private int previousTabIndex = 0;
    static GameShowServer server = new GameShowServer();
    private static Game game = new Game();

    public Server(String N) {
        super(N);
        setExtendedState(MAXIMIZED_BOTH);
        setUndecorated(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tabPane = new JTabbedPane();
        server.setGame(game);
        tabPane.addTab(StringFormatter.formatString("Profile", StringFormatter.TAB_STR, 60), new ProfileTab(server));
        tabPane.addTab(StringFormatter.formatString("Roster", StringFormatter.TAB_STR, 60), new RosterTab(server));
        tabPane.addTab(StringFormatter.formatString("Scoreboard", StringFormatter.TAB_STR, 60), new ScoreboardTab(server));
        tabPane.addTab(StringFormatter.formatString("Game", StringFormatter.TAB_STR, 60), new ResultsTab(server));
        /*try {
         tabPane.addTab("", new ImageIcon(ImageIO.read(new File("C:/Group Project/start_backup.png"))),
         new JPanel());
         } catch (IOException e) {
         e.printStackTrace();
         }*/
        tabPane.addChangeListener(this);
        add(tabPane);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Server("Server");
    }

    public static void changeGameTab() {
        GameShowServer.resetResponses();
        if (tabPane.getComponent(3) instanceof ServerGameTab) {
            try {
                server.getGame().updateScores(server.readAnswers());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JAXBException e) {
                e.printStackTrace();
            }
            tabPane.setComponentAt(3, new ServerAnswerTab(server));
        } else if (tabPane.getComponent(3) instanceof ServerAnswerTab) {
            tabPane.setComponentAt(3, new ResultsTab(server));
        } else if (tabPane.getComponent(3) instanceof ResultsTab) {
            if (initialChange) {
                game.incrementQuestionIndex();
            }else{
                initialChange=true;
            }
            tabPane.setComponentAt(3, new ServerGameTab(server));
            try {
                server.sendQuestion(game.getCurrentQuestionIndex());
            } catch (JAXBException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void changeGameTabBack() {
        if (tabPane.getComponent(3) instanceof ServerGameTab) {
            game.downQuestionIndex();
            tabPane.setComponentAt(3, new ResultsTab(server));
        } else if (tabPane.getComponent(3) instanceof ResultsTab) {
            tabPane.setComponentAt(3, new ServerAnswerTab(server));
        } else if (tabPane.getComponent(3) instanceof ServerAnswerTab) {
            tabPane.setComponentAt(3, new ServerGameTab(server));
        }
    }
    boolean pop = false;

    private void makePopUp() {
        if (pop) {
            JOptionPane.showMessageDialog(null, new JLabel(StringFormatter.formatString("Make sure you fill in all of the folder options!", StringFormatter.QUESTION_STR)));
        }
        pop = !pop;
    }

    public void stateChanged(ChangeEvent e) {

        if (tabPane.getSelectedIndex() == 4) {
            System.exit(0);
        }
        if (((ProfileTab) tabPane.getComponent(0)).optionsValid() == false) {
            tabPane.setSelectedIndex(0);
            makePopUp();
        }
        if (tabPane.getSelectedIndex() == 2) {
            ((ScoreboardTab) tabPane.getComponentAt(2)).updateScoreboard();
        }
        if (tabPane.getSelectedIndex() == 3) {
            if (tabPane.getComponent(3) instanceof ServerGameTab) {
                ((ServerGameTab) tabPane.getComponent(3)).pause();
            }
        }
        if (previousTabIndex == 3) {
            if (tabPane.getComponent(3) instanceof ServerGameTab) {
                ((ServerGameTab) tabPane.getComponent(3)).pause();
            }
        }
        previousTabIndex = tabPane.getSelectedIndex();

    }
}
